
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace Iridium
{
	public class ggSession
	{
		int _number = -1;
		bool _connected = false;
		public int ggNumber
		{
			get { return _number; }
		}
		public bool ggConnected
		{
			get { return _connected; }
		}


		/// <summary>
		/// Logowanie do sieci.
		/// </summary>
		/// <param name="number">Numer GG</param>
		/// <param name="password">Hasło</param>
		/// <returns>Zwraca -2 jeśli nie znajdzie serwera lub niepodłączony do sieci,
		/// -1 jeśli hasło lub login są nieprawidłowe,
		/// 0 w wypadku błędu wewnętrznego,
		/// 1 w wypadku udanego połączenia.</returns>
		/// <remarks>?</remarks>
		public int ggLogin(int number, string password)
		{
			string serv_str = "";
			WebClient wc = new WebClient();
			switch (ggConst.use_file)
			{
				case 3:
					serv_str = wc.DownloadString("http://appmsg.gadu-gadu.pl/appsvc/appmsg3.asp?fmnumber=" + number + "&lastmsg=0&version=" + ggConst.version_8);
					break;
				case 8:
					serv_str = wc.DownloadString("http://appmsg.gadu-gadu.pl/appsvc/appmsg_ver8.asp?fmnumber=" + number + "&lastmsg=0&version=" + ggConst.version_10);
					break;
				case 10:
					serv_str = wc.DownloadString("http://appmsg.gadu-gadu.pl/appsvc/appmsg_ver10.asp?fmnumber=" + number + "&lastmsg=0&version=" + ggConst.version_10);
					break;
				default:
					serv_str = wc.DownloadString("http://appmsg.gadu-gadu.pl/appsvc/appmsg_ver8.asp?fmnumber=" + number + "&lastmsg=0&version=" + ggConst.version_8);
					break;
			}
			string[] data = serv_str.Split(' ');

			TcpClient s = new TcpClient();
			s.Connect(data[2].Split(':')[0], 8074);

			NetworkStream ns = s.GetStream();

			System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ns);

			gg_welcome lol = new gg_welcome();
			if (lol.Receive(ns))
			{
				
				
				gg_login80 xd = new gg_login80(ns);
				xd.password = password;
				xd.uin = number;
				xd.seed = lol.Seed;

				if (xd.sendPacket(ns))
				{
					
						ggPacketToReceive d = new ggPacketToReceive();
						try
						{
							d.Receive(ns);
						}
						catch (Exception ex)
						{
							MessageBox.Show(ex.Message);
						}
						if (d.packetType == ggPacketTypes_Receive.gg_login80_failed)
						{
							MessageBox.Show("failed");
						}
						else if (d.packetType == ggPacketTypes_Receive.gg_login80_ok)
						{
							MessageBox.Show("ok");
						}

						
					

				}
				else
				{
					MessageBox.Show("sending error..");
				}


			}
			else
			{
				MessageBox.Show("What the...");
			}
			return 0; //WAZNE!!!!!!!!!!!!!
		}
	}
}